/*
https://leetcode.cn/problems/max-consecutive-ones-iii/description/
1004. 最大连续1的个数 III
medium, 李佳韵 2024.09.01
滑动窗口

*/

#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;

class Solution {
public:
    int maximumUniqueSubarray(vector<int>& nums) {
        const int N = nums.size();
        vector<int> pre(N + 1);
        for (int i = 0; i < N; i++)
            pre[i + 1] = nums[i] + pre[i];

        int left=0, right=0, res=0;
        unordered_map<int, int> mp;
        while (right < N) {
            if (mp.find(nums[right]) != mp.end() && mp[nums[right]] >= left) {
                left = mp[nums[right]] + 1;
            }
            res = max(res, pre[right + 1] - pre[left]);
            mp[nums[right]] = right;
            right++;
        }
        return res;
    }
};

int main()
{
    vector<int>nums={1,1,1,0,0,0,1,1,1,1,0};
    int k=2;
    Solution solution;
    int result=solution.maximumUniqueSubarray(nums);
    cout<<"最大连续1的个数为:"<<result<<endl;
    return 0;
}